#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<assert.h>
#include<string.h>
#include <sys/types.h>
#include <pthread.h>



#define LOG_TAG "main"

#include "jsonrpc-c.h"
#include "comm.h"
#include "cJSON.h"

char *ProgramRootPath;

//线程句柄
pthread_t HTTP_main_thread_tid;

//RPC 方法
cJSON * say_hello(jrpc_context * ctx, cJSON * params, cJSON *id) {
	return cJSON_CreateString("Hello!");
}


cJSON * add(jrpc_context * ctx, cJSON * params, cJSON *id) {
	cJSON * a = cJSON_GetArrayItem(params,0);
	cJSON * b = cJSON_GetArrayItem(params,1);
	return cJSON_CreateNumber(a->valueint + b->valueint);
}

extern struct jrpc_server my_server;

void main(int argc, char *argv[])
{
    int ret;
    //获取运行路径
    ProgramRootPath = malloc(strlen(argv[0]));
    memset(ProgramRootPath,0x00,strlen(argv[0]));
    memcpy(ProgramRootPath,argv[0],strlen(argv[0])-17);    
    printf("PATH : %s",ProgramRootPath);
   
    //创建HTTP线程
    ret = pthread_create(&HTTP_main_thread_tid,NULL,HTTP_MainThread,NULL);
    jrpc_register_procedure(&my_server, say_hello, "sayHello", NULL );
	jrpc_register_procedure(&my_server, add, "add", NULL );
    while (1)
    {
        delay_ms(2000);
    }
    
}

